/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package agen.util.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.WritableByteChannel;
import java.util.Calendar;
import java.util.Date;
import org.jdesktop.application.Application;

/**
 *
 * @author juan
 */
public class Log {
    private static File log;
    private static WritableByteChannel logChannel;
    private static ByteBuffer logByteBuf;
    private static FileOutputStream logFileWrapper;

    /**independientemente del nombre, la idea es guardarlo en el mismo dir que la
     aplicacion, a menos que nombreOpcional sea absoluto, mandar null para usar
     la nomencaltura por defecto*/
    public static void crearArchivoLog(String nombreOpcional){
        try {
        if (nombreOpcional == null) {
            Date date =  new Date();
            date.setTime(System.currentTimeMillis());
            /*Mientraz nunca termine unaiteracion antes de que pase el milisegunto
             no hay drama*/
            logFileWrapper = new FileOutputStream("Ejecucion_" + System.currentTimeMillis() + ".log");
        } else {
            logFileWrapper = new FileOutputStream(nombreOpcional);
        }
        
          logChannel = logFileWrapper.getChannel();
           // Create a direct ByteBuffer;
           logByteBuf = ByteBuffer.allocateDirect(10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void escribirLinea(String linea){
        try {
            int count = 0;
            int index = 0;
            linea += "\n";
            byte[] bytes = linea.getBytes();
            count = bytes.length;
            // Continue writing bytes until there are no more
            while (index < bytes.length) {
                // Fill ByteBuffer
                while (index < count && logByteBuf.hasRemaining()) {
                    logByteBuf.put(bytes[index++]);
                }

                // Set the limit to the current position and
                // the position to 0 making the new bytes
                // visible for write()
                logByteBuf.flip();

                // Write the bytes to the channel
                int numWritten = logChannel.write(logByteBuf);

                // Check if all bytes were written
                if (logByteBuf.hasRemaining()) {
                    // If not all bytes were written, move the unwritten bytes
                    // to the beginning and set position just after the last
                    // unwritten byte; also set limit to the capacity
                    logByteBuf.compact();
                } else {
                    // Set the position to 0 and the limit to capacity
                    logByteBuf.clear();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void cerrarArchivo(){
        try {
            logChannel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
